﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.UserControl;
using BestBuy.Common;
namespace BestBuy.PageUser
{
    public partial class CopyProduct : System.Web.UI.Page
    {
        private static int idselect;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!Page.IsPostBack)
            {
                LoadDonVi();
                if (Request.Params["IdProduct"] != null)
                {
                    LoadInfoProduct(int.Parse(Request.Params["IdProduct"].ToString()));
                    HiddenCategory.Value = Request.Params["IdProduct"].ToString();
                }
                loadList1(List1, null);
            }
        }

        private void LoadDonVi()
        {
            cbDonvi1.DataSource = cDonVi.getDonVi();
            cbDonvi1.DataTextField = "TenDonVi";
            cbDonvi1.DataValueField = "IDDonVi";
            cbDonvi1.DataBind();

            cbDonVi2.DataSource = cDonVi.getDonVi();
            cbDonVi2.DataTextField = "TenDonVi";
            cbDonVi2.DataValueField = "IDDonVi";
            cbDonVi2.DataBind();
        }
        private void LoadInfoProduct(int idProduct)
        {
            SanPham sp = cManageProduct.getProduct(idProduct);
            lbDanhMuc.Text = cManageProduct.getCategoryName(sp.IDDanhMuc);
            HiddenCategory.Value = sp.IDDanhMuc.ToString();
            txtProductDetail.Text = sp.ChiTietSanPham;
            txtProductName.Text = sp.TenSanPham;
            txtModel.Text = sp.MaLoaiSanPham;
            txtKeyword.Text = sp.TuKhoa;
            txtDescription.Text = sp.MoTa;            
            StartTimeTextBox.Text = sp.KyHanGiaoHang;
            String[] str = sp.KhaNangCungCap.Split('/');
            EndTimeTextBox.Text = str[0];
            cbDonvi1.SelectedItem.Text = str[1];
            cbDay.Text = str[2];
            txtPlaceOfOrigin.Text = sp.NoiXuatXu;
            String[] str1 = sp.SoLuongDatToiThieu.Split('/');
            txtMinimumOrder.Text = str1[0];
            cbDonVi2.SelectedItem.Text = str1[1];
            String[] str3 = sp.PhuongThucThanhToan.Split('+');
            if (str3[0] == "1")
            {
                CheckBox1.Checked = true;
            }
            if (str3[1] == "1")
            {
                CheckBox2.Checked = true;
            }
            if (str3[2] == "1")
            {
                CheckBox3.Checked = true;
            }
            if (str3[3] == "1")
            {
                CheckBox4.Checked = true;
            }                        
        }

        protected void btnAddCategory_OnClick(object sender, EventArgs e)
        {
            if (cManageCategory.countChildren(idselect)==0)
            {
                lbDanhMuc.Text = cManageCategory.getNameCategory(idselect);
                HiddenCategory.Value = idselect.ToString();
            }
            else
            {
                lbThongBaoError.Visible = true;                
                mpeDanhMuc.Show();
            }            
        }

        protected void btnCancel_OnClick(object sender, EventArgs e)
        {
            lbThongBaoAddCategory.Visible = false;
            lbThongBaoAddCategory.Text = "";
        }

        private void loadList1(ListBox list, int? id)
        {
            List1.DataSource = cManageCategory.GetChildrenOfCategory(id);
            List1.DataTextField = "TenDanhMuc";
            List1.DataValueField = "IDDanhMuc";
            List1.DataBind();
        }
        protected void List1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.mpeDanhMuc.Show();
            int id = int.Parse(List1.SelectedItem.Value);
            List3.DataSource = null;
            List3.DataBind();
            List2.DataSource = cManageCategory.GetChildrenOfCategory(id);
            List2.DataTextField = "TenDanhMuc";
            List2.DataValueField = "IDDanhMuc";
            List2.DataBind();
            idselect = id;
            
           
        }
        protected void List2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(List2.SelectedItem.Value);
            List3.DataSource = cManageCategory.GetChildrenOfCategory(id);
            List3.DataTextField = "TenDanhMuc";
            List3.DataValueField = "IDDanhMuc";
            List3.DataBind();
            this.mpeDanhMuc.Show();
            idselect = id;
        }
        protected void List3_SelectedIndexChanged(object sender, EventArgs e)
        {
            idselect = int.Parse(List3.SelectedItem.Value);
            this.mpeDanhMuc.Show();
        }
        protected void lbtnChangeCategory_Click(object sender, EventArgs e)
        {
            loadList1(List1, null);
            this.mpeDanhMuc.Show();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SanPham sp = new SanPham();
            sp.IDDanhMuc = int.Parse(HiddenCategory.Value);
            sp.ChiTietSanPham = txtProductDetail.Text;
            sp.TenSanPham = txtProductName.Text;
            sp.MaLoaiSanPham = txtModel.Text;
            sp.TuKhoa = txtKeyword.Text;
            sp.MoTa = txtDescription.Text;
            if (uploadPhoto.HasFile)
            {
                sp.HinhAnh = uploadPhoto.FileName;
            }
            else
            {
                sp.HinhAnh = "";
                String strPhoto = Server.MapPath("~/Images/Product/") + uploadPhoto.FileName;
                uploadPhoto.SaveAs(strPhoto);
            }
            if (upLoadBrochoure.HasFile)
            {
                sp.TaiLieu = upLoadBrochoure.FileName;
                String str = "";
                str = Server.MapPath("~/Brochoure/") + upLoadBrochoure.FileName;
                upLoadBrochoure.SaveAs(str);
            }
            else
            {
                sp.TaiLieu = "";
            }
            sp.KyHanGiaoHang = StartTimeTextBox.Text;
            sp.KhaNangCungCap = EndTimeTextBox.Text + "/" + cbDonvi1.SelectedItem.Text + "/" + cbDay.Text;
            sp.NoiXuatXu = txtPlaceOfOrigin.Text;
            sp.SoLuongDatToiThieu = txtMinimumOrder.Text + "/" + cbDonVi2.SelectedItem.Text;
            int[] paymentID = { 0, 0, 0, 0 };
            if (CheckBox1.Checked == true)
            {
                paymentID[0] = 1;
            }
            if (CheckBox2.Checked == true)
            {
                paymentID[1] = 1;
            }
            if (CheckBox3.Checked == true)
            {
                paymentID[2] = 1;
            }
            if (CheckBox4.Checked == true)
            {
                paymentID[3] = 1;
            }
            string strPayment = "";
            for (int i = 0; i < paymentID.Count(); i++)
            {
                strPayment += paymentID[i].ToString() + "+";
            }
            sp.PhuongThucThanhToan = strPayment;
            sp.IDTaiKhoan = int.Parse(Session["IDDN"].ToString());
            sp.TinhTrang = (int)SanPhamStatus.Unverified;
            sp.NgayDang = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
            cDangSanPham.AddProduct(sp);
            Response.Redirect("~/PageUser/MbSellling.aspx");
        }
    }
}